package com.hzit.manager.config;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import javax.annotation.PostConstruct;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
// @Configuration
public class HzitWebCallbackManager {

	// @PostConstruct      // 在构造方法执行完成后自动执行
	// public void init(){
	// 	WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
	// 		@Override
	// 		public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
	// 			response.setContentType("text/application;charset=utf8");
	// 			PrintWriter writer = response.getWriter();
	// 			R<Object> r = R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "出现了限流错误！");
	// 			writer.println(JSON.toJSONString(r));
	// 			writer.flush();
	// 		}
	// 	});
	// }
	// @Bean
	// public FilterRegistrationBean sentinelFilterRegistration() {
	// 	FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
	// 	registration.setFilter(new CommonFilter());
	// 	registration.addUrlPatterns("/*");
	// 	registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
	// 	registration.setName("sentinelFilter");
	// 	registration.setOrder(1);
	//
	// 	return registration;
	// }
}
